import 'dart:ui';

import 'package:flutter_app/constant/const_string.dart';
import 'package:flutter_app/pages/white_board/white_board_vm.dart';
import 'package:flutter_app/utils/paint_util.dart';

enum ScaleLayerWidgetType { zoomIn, zoomOut }

extension ScaleLayerLogic on WhiteBoardVM {
  void aroundCenterScale({
    required Offset center,
    required ScaleLayerWidgetType type,
    double stepScale = 0.1,
  }) {
    if (type == ScaleLayerWidgetType.zoomOut) {
      // 中心缩小画布
      if (double.parse(curCanvasScale.toStringAsFixed(1)) <= minCanvasScale) {
        preCanvasScale = curCanvasScale;
        curCanvasScale = minCanvasScale;
      } else {
        preCanvasScale = curCanvasScale;
        curCanvasScale -= stepScale;
      }
    } else if (type == ScaleLayerWidgetType.zoomIn) {
      // 中心放大画布
      if (double.parse(curCanvasScale.toStringAsFixed(1)) >= maxCanvasScale) {
        preCanvasScale = curCanvasScale;
        curCanvasScale = maxCanvasScale;
      } else {
        preCanvasScale = curCanvasScale;
        curCanvasScale += stepScale;
      }
    }

    curCanvasOffset += PaintUtil.centerZoomAlgorithm(
      center: center,
      curCanvasOffset: curCanvasOffset,
      curCanvasScale: curCanvasScale,
      preCanvasScale: preCanvasScale,
    );

    update(
      [ConstString.gridBackgroundWidgetId, ConstString.graphicsLayerWidgetId],
    );
  }
}
